package com.example.administrator.wallet.view.activity.mine.Personal.ReceiptInformation.BankCards;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.wallet.R;
import com.example.administrator.wallet.constant.Api;
import com.example.administrator.wallet.intf.IRequestCallback;
import com.example.administrator.wallet.intf.IRequestManager;
import com.example.administrator.wallet.model.bean.AddProceedBean;
import com.example.administrator.wallet.model.bean.DelProceedBean;
import com.example.administrator.wallet.model.bean.GetOneBean;
import com.example.administrator.wallet.utils.RequestFactory;
import com.example.administrator.wallet.utils.SharedPrefsUtil;
import com.example.administrator.wallet.view.activity.mine.Personal.ReceiptInformation.ReceiptInformationActivity;
import com.google.gson.Gson;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.OkHttpFinal;
import cn.finalteam.okhttpfinal.OkHttpFinalConfiguration;
import cn.finalteam.okhttpfinal.RequestParams;
import okhttp3.Headers;

public class ModifyBankCardsActivity extends AppCompatActivity {
    private ImageButton mIb_back;
    private TextView mV_title,tv_bank_open;
    private EditText ed_bankcards_name,ed_bankcards_number;
    private RelativeLayout rl_select_bank;
    private Button bt_finish,bt_delete;
    private RequestParams params;

    private String id = "";
    private DelProceedBean delProceedBean;
    private GetOneBean getOneBean;
    private String bank_name;
    final IRequestManager reqequestManager = RequestFactory.getReqequestManager();
    private int value = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_bank_cards);

        setStatuColor();


        //传递dataBean
        Intent intent = getIntent();
        id = intent.getStringExtra("id");


        mIb_back = (ImageButton) findViewById(R.id.finish_img);
        mV_title = (TextView) findViewById(R.id.view_title);
        ed_bankcards_name = (EditText) findViewById(R.id.ed_bankcards_name);
        ed_bankcards_number = (EditText) findViewById(R.id.ed_bankcards_number);
        tv_bank_open = (TextView) findViewById(R.id.tv_bank_open);
        rl_select_bank = (RelativeLayout) findViewById(R.id.rl_select_bank);
        bt_finish = (Button) findViewById(R.id.bt_finish);
        bt_delete = (Button) findViewById(R.id.bt_delete);

        mIb_back.setVisibility(View.VISIBLE);
        mV_title.setText(R.string.ReceiptInfo);

        bank_name = SharedPrefsUtil.getValue(ModifyBankCardsActivity.this, "wallet", "bank_name", "");
        if ("".equals(bank_name)){
        }else{
            tv_bank_open.setText(bank_name);
            ed_bankcards_name.setText(SharedPrefsUtil.getValue(ModifyBankCardsActivity.this, "wallet", "ed_bankcards_name", ""));
            ed_bankcards_number.setText(SharedPrefsUtil.getValue(ModifyBankCardsActivity.this, "wallet", "ed_bankcards_number", ""));

            SharedPrefsUtil.putValue(ModifyBankCardsActivity.this, "wallet", "ed_bankcards_name","");
            SharedPrefsUtil.putValue(ModifyBankCardsActivity.this, "wallet", "ed_bankcards_number","");
            SharedPrefsUtil.putValue(ModifyBankCardsActivity.this, "wallet", "bank_name","");
            value = 1;
        }
        if (value == 0){
            value = 1;
            reqequestManager.httpPost(Api.BaseUrl + Api.Get_One, Api.getGetOne(SharedPrefsUtil.getValue(ModifyBankCardsActivity.this, "wallet", "token", ""),id), new IRequestCallback() {
                @Override
                public void onSuccess(String Response) {
                    Gson gson = new Gson();
                    getOneBean = gson.fromJson(Response, GetOneBean.class);
                    if (getOneBean.getCode() == 200){
                        ed_bankcards_name.setText(getOneBean.getData().getUsername());
                        ed_bankcards_number.setText(getOneBean.getData().getAccount());
                        tv_bank_open.setText(getOneBean.getData().getBank_name());

                        SharedPrefsUtil.putValue(ModifyBankCardsActivity.this, "wallet", "ed_bankcards_name",getOneBean.getData().getUsername());
                        SharedPrefsUtil.putValue(ModifyBankCardsActivity.this, "wallet", "ed_bankcards_number",getOneBean.getData().getAccount());
                        SharedPrefsUtil.putValue(ModifyBankCardsActivity.this, "wallet", "bank_id",id);
                    }else{
                        Toast.makeText(getApplicationContext(),getOneBean.getMessage(),Toast.LENGTH_SHORT).show();
                        onBackPressed();
                    }
                }
                @Override
                public void onFailure(String error) {
                }
            });
        }


        rl_select_bank.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getApplicationContext(),SelectBankActivity.class);
                i.putExtra("context","ModifyBankCardsActivity");
                startActivity(i);
//                finish();
                overridePendingTransition(R.anim.next_in_anim, R.anim.next_out_anim);
            }
        });
        //删除
        bt_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final IRequestManager reqequestManager = RequestFactory.getReqequestManager();
                reqequestManager.httpPost(Api.BaseUrl + Api.Del_Proceed, Api.getDel_Proceed(SharedPrefsUtil.getValue(ModifyBankCardsActivity.this, "wallet", "token", ""),id), new IRequestCallback() {
                    @Override
                    public void onSuccess(String Response) {
                        Gson gson = new Gson();
                        delProceedBean = gson.fromJson(Response, DelProceedBean.class);
                        if (delProceedBean.getCode() == 200){
                            Toast.makeText(getApplicationContext(),delProceedBean.getMessage(),Toast.LENGTH_SHORT).show();
                            onBackPressed();
                        }else{
                            Toast.makeText(getApplicationContext(),delProceedBean.getMessage(),Toast.LENGTH_SHORT).show();
                        }
                    }
                    @Override
                    public void onFailure(String error) {
                    }
                });
            }
        });
        //完成
        bt_finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (id == null){
                    id = SharedPrefsUtil.getValue(ModifyBankCardsActivity.this, "wallet", "bank_id", "");
                }
                OkHttpFinalConfiguration.Builder builder = new OkHttpFinalConfiguration.Builder();
                OkHttpFinal.getInstance().init(builder.build());
                params = new RequestParams();
                params.addFormDataPart("access_token", SharedPrefsUtil.getValue(ModifyBankCardsActivity.this, "wallet", "token", ""));
                params.addFormDataPart("proceeds_type","bank");
                params.addFormDataPart("id",id);
                params.addFormDataPart("account",ed_bankcards_number.getText().toString());
                params.addFormDataPart("username",ed_bankcards_name.getText().toString());
                params.addFormDataPart("bank_name",tv_bank_open.getText().toString());//tv_bank_open.getText().toString();   "银行卡开户行".equals(tv_bank_open.getText().toString())
                if ("".equals(ed_bankcards_number.getText().toString()) | "".equals(ed_bankcards_name.getText().toString()) | "银行卡开户行".equals(tv_bank_open.getText().toString())){
                    Toast.makeText(getApplicationContext(), R.string.Submissioncannotbeempty,Toast.LENGTH_SHORT).show();
                }else{
                    //银行卡信息修改
                    HttpRequest.post("https://wallet.kinlink.cn/api/gathering/up-proceed",params,new BaseHttpRequestCallback<AddProceedBean>(){
                        @Override
                        protected void onSuccess(Headers headers, AddProceedBean addProceedBean) {
                            super.onSuccess(headers, addProceedBean);
                            int code = addProceedBean.getCode();
                            if (code == 200){
                                Toast.makeText(getApplicationContext(),addProceedBean.getMessage(),Toast.LENGTH_SHORT).show();
                                onBackPressed();
                            }else {
                                Toast.makeText(getApplicationContext(),addProceedBean.getMessage(),Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFailure(int errorCode, String msg) {
                            super.onFailure(errorCode, msg);

                        }
                    });
                }
            }
        });

        mIb_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    public void onBackPressed() {
        super.onBackPressed();
        startActivity(new Intent(getApplicationContext(),ReceiptInformationActivity.class));
        finish();
        overridePendingTransition(R.anim.pre_in_anim, R.anim.pre_out_anim);
    }
    public void setStatuColor() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(android.R.color.white));
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        setMiuiStatusBarDarkMode(this, true);
        setMiuiStatusBarDarkMode(this, true);
    }

    //设置成白色的背景，字体颜色为黑色。
    public static boolean setMiuiStatusBarDarkMode(Activity activity, boolean darkmode) {
        Class<? extends Window> clazz = activity.getWindow().getClass();
        try {
            int darkModeFlag = 0;
            Class<?> layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
            Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
            darkModeFlag = field.getInt(layoutParams);
            Method extraFlagField = clazz.getMethod("setExtraFlags", int.class, int.class);
            extraFlagField.invoke(activity.getWindow(), darkmode ? darkModeFlag : 0, darkModeFlag);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean setMeizuStatusBarDarkIcon(Activity activity, boolean dark) {
        boolean result = false;
        if (activity != null) {
            try {
                WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
                Field darkFlag = WindowManager.LayoutParams.class
                        .getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                Field meizuFlags = WindowManager.LayoutParams.class
                        .getDeclaredField("meizuFlags");
                darkFlag.setAccessible(true);
                meizuFlags.setAccessible(true);
                int bit = darkFlag.getInt(null);
                int value = meizuFlags.getInt(lp);
                if (dark) {
                    value |= bit;
                } else {
                    value &= ~bit;
                }
                meizuFlags.setInt(lp, value);
                activity.getWindow().setAttributes(lp);
                result = true;
            } catch (Exception e) {
            }
        }
        return result;
    }

}
